package while_lang.ast;

import java.util.Map;
import java.util.Random;

/** Nodo del AST que representa una sentencia de iteración o bucle.
 *  @author Leonardo Val
 */
public class WhileDo extends Stmt {	
	public final Exp condition;
	public final Stmt body;
	
	public WhileDo(Exp condition, Stmt body) {
		this.condition = condition;
		this.body = body;
	}

	@Override public String unparse() {
		return "while "+ condition.unparse() +" do "+ body.unparse();
	}

	@Override public void halteadNumbers(Map<String, Integer> operands, Map<String, Integer> operators) {
		condition.halteadNumbers(operands, operators);
		body.halteadNumbers(operands, operators);

		String key = "WhileDo";
		Stmt.incrementarDiccionario(key, operators);

	}
	
	public static WhileDo generate(Random random, int min, int max) {
		return new WhileDo(
			Exp.generate(random, min-1, max-1),
			Stmt.generate(random, min-1, max-1)
		);
	}
	
	@Override public String toString() {
		return "WhileDo(" + condition + ", " + body + ")";
	}

	@Override public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((body == null) ? 0 : body.hashCode());
		result = prime * result
				+ ((condition == null) ? 0 : condition.hashCode());
		return result;
	}

	@Override public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		WhileDo other = (WhileDo) obj;
		if (body == null) {
			if (other.body != null)
				return false;
		} else if (!body.equals(other.body))
			return false;
		if (condition == null) {
			if (other.condition != null)
				return false;
		} else if (!condition.equals(other.condition))
			return false;
		return true;
	}
	
}
